//
//  MineHomCell.swift
//  DKE-Album
//
//  Created by 杨东宝 on 2020/5/29.
//  Copyright © 2020 杨东宝. All rights reserved.
//

import UIKit

class MineHomCell: UICollectionViewCell {
    
    //MARK:-图标
    lazy var iconImage: UIImageView = {
        let iconView = UIImageView()
        iconView.image = imageNamed("menu_image1")
        contentView.addSubview(iconView)
        return iconView
    }()
    //MARK:-文字
    lazy var textLabel: UILabel = {
        let label = UILabel()
        label.text = "学习设置"
        label.font = DIMENSION_FONT_OF_SIZE(12)
        label.textColor = hexColor(0x333333,0xBBBBBB)
        contentView.addSubview(label)
        return label
    }()
    //MARK:-未读
    lazy var unReadLabel: UILabel = {
        let label = UILabel()
        label.text = "0"
        label.font = DIMENSION_FONT_OF_SIZE(12)
        label.textColor = hexColor(0xffffff,0xffffff)
        label.backgroundColor = hexColor(0xE02020,0xE02020)
        label.sizeToFit()
        label.layer.cornerRadius = DIMENSION_VALUE(8)
        label.layer.masksToBounds = true
        label.textAlignment = .center
        label.isHidden = true
        contentView.addSubview(label)
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
        //未读消息数量
        NotificationCenter.default.addObserver(self, selector: #selector(unreadMessageCount(_ :)), name:  NSNotification.Name(rawValue: KNotificationUnreadMsg), object: nil)
        initSubViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initSubViews() {
        
        iconImage.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(DIMENSION_VALUE(14))
            make.width.height.equalTo(DIMENSION_VALUE(24))
            make.centerX.equalTo(contentView.snp.centerX)
        }
        
        textLabel.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(DIMENSION_VALUE(-14))
            make.height.equalTo(DIMENSION_VALUE(20))
            make.centerX.equalTo(contentView.snp.centerX)
        }
        unReadLabel.snp.makeConstraints { (make) in
            make.height.width.equalTo(DIMENSION_VALUE(16))
            make.centerX.equalTo(iconImage.snp.right)
            make.centerY.equalTo(iconImage.snp.top)
        }  
    }
    //MARK:-设置数据
    func setDict(dict:[String:String]?) {
        iconImage.image = imageNamed(dict?["icon"] ?? "menu_image1")
        textLabel.text = dict?["title"] ?? "默认"
    }
    //MARK:-消息的数量通知
    @objc func unreadMessageCount(_ result:Notification) {
        
        guard let dict = result.userInfo else { return }
        
        guard let unreadCount = dict["unreadCount"] else { return }
        
        if textLabel.text == "消息中心" {
            if (unreadCount as! String  == "0") {
                unReadLabel.isHidden = true
            }else{
                unReadLabel.isHidden = false
                unReadLabel.text = unreadCount as? String
            }
        }
    }
}
